package com.android.delivery.activity.wangdian

import androidx.recyclerview.widget.RecyclerView
import com.android.delivery.activity.fragment.WangDianFragment
import com.android.delivery.adapter.DaiJieCheAdapter
import com.android.delivery.api.ApiService
import com.android.delivery.base.BaseListFragmet
import com.android.delivery.callback.DialogCallback
import com.android.delivery.callback.LzyResponse
import com.android.delivery.entity.Arrived
import com.android.delivery.entity.even.RefreshConfirmJieChe
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.include_refresh_layout.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * 待接车列表
 */
class DaiJieCheListFragment : BaseListFragmet<Arrived>() {

    private val daiJieCheAdapter: DaiJieCheAdapter by lazy {
        DaiJieCheAdapter()
    }

    companion object {
        val DETAIL_REQUEST_CODE: Int = 1

        fun newInstance(): DaiJieCheListFragment {
            val fragment = DaiJieCheListFragment()
            return fragment
        }
    }

    override fun bindRecyclerView(recLv: RecyclerView?) {
        recLv!!.adapter = daiJieCheAdapter
    }

    override fun loadData(page: Int, pageCount: Int) {
        ApiService.order_arrived(this, page, object : DialogCallback<LzyResponse<List<Arrived>>>() {
            override fun onSuccess(response: Response<LzyResponse<List<Arrived>>>?) {
                finishRefreshaLayout()
                onGetDataSuc(response!!.body().data)
            }

            override fun onError(response: Response<LzyResponse<List<Arrived>>>?) {
                super.onError(response)
                finishRefreshaLayout()
            }
        })
    }

    override fun getEnableRefresh(): Boolean {
        return false
    }

    override fun getData(): MutableList<Arrived> {
        return daiJieCheAdapter.data
    }

    override fun bindData(isRefresh: Boolean, list: MutableList<Arrived>?) {
        if (isRefresh) daiJieCheAdapter.setNewData(list) else daiJieCheAdapter.addData(list!!)
    }

    override fun isBindEventBus(): Boolean {
        return true
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun refresh(refresh: RefreshConfirmJieChe) {
        onRefresh(refreshLayout)
    }

    fun setEnableRefresh(enableRefresh: Boolean) {
        if (refreshLayout != null) {
            refreshLayout.setEnableRefresh(false)
        }
    }

    fun onAutoRefresh() {
        onRefresh(refreshLayout)
    }

    /**
     * 结束下拉刷新
     */
    private fun finishRefreshaLayout() {
    }
}